/**
 * Peycash. Todos los derechos reservados.
 * Sistema de Env&iacute;o de Dinero para la red de establecimientos Peycash.
 */
package com.peycash.envio.endpoint;

import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;

import com.peycash.envio.dto.R001;
import com.peycash.envio.dto.T001;

/**
 * Clase que contiene el End Point de Web Service de Env&iacute;o de Dinero.
 * 
 * @author TEKNEI - Rabelt Ibarra Godinez.
 */
public class ProcesoEnvioEndPoint extends AbstractJDomPayloadEndpoint {

        private PeyCashISOUtilsService isoutils;

        /*
         * (non-Javadoc)
         * 
         * @see
         * org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint
         * #invokeInternal(org.jdom.Element)
         */
        public final Element invokeInternal(final Element requestElement) {
                Element result = procesa(requestElement);
                return result;
        }

        private Element procesa(Element root) {
                T001 t001 = Translator.translate(root.getChildren());
                ISOMsg m = null;
                ISOMsg r = null;
                R001 r001 = null;
                try {
                        m = isoutils.translateEnvio(t001);
                        r = isoutils.sendMessage(m);
                        r001 = isoutils.translateMessage(r);
                } catch (ISOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                /* processing t001 */
                if (r001 == null) {
                        r001 = new R001();
                        r001.setR1("00");
                        r001.setR3("00");
                        r001.setR4("00");
                        r001.setR5("00");
                        r001.setR6("00");
                        r001.setR7("00");
                        r001.setR8("00");
                        r001.setR9("00");
                        r001.setR10("00");
                }
                Element element = crearResponse(r001);
                return element;
        }

        /**
         * A partir de un objeto de tipo R001 (Respuesta de Transacci&oacute;n),
         * genera un objeto de tipo Element que ser&aacute; la respuesta del
         * servicio web
         * 
         * @param r001
         *                Objeto cuyas propiedades corresponden a la trama R001
         *                (Respuesta de Transacci&oacute;n).
         * @return Objeto de tipo Element que ser&aacute; la respuesta del
         *         servicio web.
         */
        private Element crearResponse(R001 r001) {
                Namespace xml = Namespace.getNamespace("xhtml",
                                "http://187.174.207.114:8080/envio711/schemas");
                Element elmRoot = new Element("TransactionResponse", xml);
                elmRoot.addContent(new Element("r1", xml).setText(r001.getR1()));
                elmRoot.addContent(new Element("r2", xml).setText(r001.getR2()));
                elmRoot.addContent(new Element("r3", xml).setText(r001.getR3()));
                elmRoot.addContent(new Element("r4", xml).setText(r001.getR4()));
                elmRoot.addContent(new Element("r5", xml).setText(r001.getR5()));
                elmRoot.addContent(new Element("r6", xml).setText(r001.getR6()));
                elmRoot.addContent(new Element("r7", xml).setText(r001.getR7()));
                elmRoot.addContent(new Element("r8", xml).setText(r001.getR8()));
                elmRoot.addContent(new Element("r9", xml).setText(r001.getR9()));
                elmRoot.addContent(new Element("r10", xml).setText(r001
                                .getR10()));
                Document doc = new Document(elmRoot);
                Element elemntDoc = doc.getRootElement();
                return elemntDoc;
        }

        /**
         * @return the isoutils
         */
        public PeyCashISOUtilsService getIsoutils() {
                return isoutils;
        }

        /**
         * @param isoutils
         *                the isoutils to set
         */
        public void setIsoutils(PeyCashISOUtilsService isoutils) {
                this.isoutils = isoutils;
        }

}
